The data set contains information about three species of IRIS flowers namely:
Four features are collected from each sample, sepal-length, sepal-width, petal-length and petal-width in centi-meters.
# Common imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.express as px
iris_df = pd.read_csv("./data/IRIS.csv.gz", compression="gzip")
iris_df.sample(5)
| Unnamed: 0 | sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|---|
| 144 | 144 | 6.7 | 3.3 | 5.7 | 2.5 | Iris-virginica |
| 48 | 48 | 5.3 | 3.7 | 1.5 | 0.2 | Iris-setosa |
| 52 | 52 | 6.9 | 3.1 | 4.9 | 1.5 | Iris-versicolor |
| 23 | 23 | 5.1 | 3.3 | 1.7 | 0.5 | Iris-setosa |
| 78 | 78 | 6.0 | 2.9 | 4.5 | 1.5 | Iris-versicolor |
fig = px.scatter_3d(iris_df, x='sepal_length', y='sepal_width', z='petal_width',
color='species', template="plotly_dark")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
covid_df = pd.read_csv("./data/owid-covid-data.csv.gz", compression="gzip")
covid_df.sample(5)
| Unnamed: 0 | iso_code | continent | location | date | total_cases | new_cases | new_cases_smoothed | total_deaths | new_deaths | ... | female_smokers | male_smokers | handwashing_facilities | hospital_beds_per_thousand | life_expectancy | human_development_index | excess_mortality_cumulative_absolute | excess_mortality_cumulative | excess_mortality | excess_mortality_cumulative_per_million | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 81551 | 81551 | KWT | Asia | Kuwait | 2020-10-08 | 109441.0 | 698.0 | 537.857 | 642.0 | 3.0 | ... | 2.7 | 37.0 | NaN | 2.0 | 75.49 | 0.806 | NaN | NaN | NaN | NaN |
| 22911 | 22911 | BRN | Asia | Brunei | 2020-09-11 | 145.0 | 0.0 | 0.000 | 3.0 | 0.0 | ... | 2.0 | 30.9 | NaN | 2.7 | 75.86 | 0.838 | NaN | NaN | NaN | NaN |
| 23353 | 23353 | BRN | Asia | Brunei | 2021-11-27 | 14992.0 | 74.0 | 78.286 | 97.0 | 0.0 | ... | 2.0 | 30.9 | NaN | 2.7 | 75.86 | 0.838 | NaN | NaN | NaN | NaN |
| 109821 | 109821 | NER | Africa | Niger | 2020-12-16 | 2361.0 | 0.0 | 40.429 | 82.0 | 0.0 | ... | 0.1 | 15.4 | 8.978 | 0.3 | 62.42 | 0.394 | NaN | NaN | NaN | NaN |
| 145776 | 145776 | SYR | Asia | Syria | 2021-09-15 | 29974.0 | 169.0 | 146.000 | 2097.0 | 7.0 | ... | NaN | NaN | 70.598 | 1.5 | 72.70 | 0.567 | NaN | NaN | NaN | NaN |
5 rows × 68 columns
required_columns = ["iso_code", "location", "continent", "date", "new_cases_smoothed", "total_cases"]
covid_df = covid_df.dropna(subset = required_columns)
covid_df = covid_df.sort_values("date")
covid_df[['iso_code', 'location']].sample(8)
| iso_code | location | |
|---|---|---|
| 37754 | CUW | Curacao |
| 19499 | BES | Bonaire Sint Eustatius and Saba |
| 113350 | NOR | Norway |
| 63674 | GIN | Guinea |
| 14759 | BLR | Belarus |
| 732 | AFG | Afghanistan |
| 15405 | BEL | Belgium |
| 48293 | SWZ | Eswatini |
covid_day_df = covid_df[covid_df.date == "2021-12-23"]
covid_day_df.sample(5)
| Unnamed: 0 | iso_code | continent | location | date | total_cases | new_cases | new_cases_smoothed | total_deaths | new_deaths | ... | female_smokers | male_smokers | handwashing_facilities | hospital_beds_per_thousand | life_expectancy | human_development_index | excess_mortality_cumulative_absolute | excess_mortality_cumulative | excess_mortality | excess_mortality_cumulative_per_million | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 98588 | 98588 | MEX | North America | Mexico | 2021-12-23 | 3940401.0 | 3319.0 | 1876.571 | 298359.0 | 198.0 | ... | 6.9 | 21.4 | 87.847 | 1.38 | 75.05 | 0.779 | NaN | NaN | NaN | NaN |
| 97077 | 97077 | MRT | Africa | Mauritania | 2021-12-23 | 40308.0 | 69.0 | 49.143 | 860.0 | 1.0 | ... | NaN | NaN | 15.950 | NaN | 64.92 | 0.546 | NaN | NaN | NaN | NaN |
| 149550 | 149550 | TLS | Asia | Timor | 2021-12-23 | 19833.0 | 0.0 | 0.429 | 122.0 | 0.0 | ... | 6.3 | 78.1 | 28.178 | 5.90 | 69.50 | 0.606 | NaN | NaN | NaN | NaN |
| 94423 | 94423 | MDV | Asia | Maldives | 2021-12-23 | 94444.0 | 145.0 | 116.429 | 261.0 | 1.0 | ... | 2.1 | 55.0 | 95.803 | NaN | 78.92 | 0.740 | NaN | NaN | NaN | NaN |
| 88477 | 88477 | LTU | Europe | Lithuania | 2021-12-23 | 507916.0 | 1772.0 | 1467.571 | 7223.0 | 23.0 | ... | 21.3 | 38.0 | NaN | 6.56 | 75.93 | 0.882 | NaN | NaN | NaN | NaN |
5 rows × 68 columns
fig = px.scatter_geo(covid_day_df, locations="iso_code", color="continent",
hover_name="location", size="new_cases_smoothed",
projection="natural earth", template="plotly_dark")
fig.show()
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) c:\Users\cheta\IDSassignment\writing-assignment-chetan_shubh\_notebooks\demo.ipynb Cell 14' in <module> <a href='vscode-notebook-cell:/c%3A/Users/cheta/IDSassignment/writing-assignment-chetan_shubh/_notebooks/demo.ipynb#ch0000013?line=0'>1</a> fig = px.scatter_geo(covid_day_df, locations="iso_code", color="continent", <a href='vscode-notebook-cell:/c%3A/Users/cheta/IDSassignment/writing-assignment-chetan_shubh/_notebooks/demo.ipynb#ch0000013?line=1'>2</a> hover_name="location", size="new_cases_smoothed", <a href='vscode-notebook-cell:/c%3A/Users/cheta/IDSassignment/writing-assignment-chetan_shubh/_notebooks/demo.ipynb#ch0000013?line=2'>3</a> projection="natural earth", template="plotly_dark") ----> <a href='vscode-notebook-cell:/c%3A/Users/cheta/IDSassignment/writing-assignment-chetan_shubh/_notebooks/demo.ipynb#ch0000013?line=4'>5</a> fig.show() File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\plotly\basedatatypes.py:3400, in BaseFigure.show(self, *args, **kwargs) <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/basedatatypes.py?line=3366'>3367</a> """ <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/basedatatypes.py?line=3367'>3368</a> Show a figure using either the default renderer(s) or the renderer(s) <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/basedatatypes.py?line=3368'>3369</a> specified by the renderer argument (...) <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/basedatatypes.py?line=3395'>3396</a> None <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/basedatatypes.py?line=3396'>3397</a> """ <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/basedatatypes.py?line=3397'>3398</a> import plotly.io as pio -> <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/basedatatypes.py?line=3399'>3400</a> return pio.show(self, *args, **kwargs) File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\plotly\io\_renderers.py:397, in show(fig, renderer, validate, **kwargs) <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/io/_renderers.py?line=391'>392</a> raise ValueError( <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/io/_renderers.py?line=392'>393</a> "Mime type rendering requires ipython but it is not installed" <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/io/_renderers.py?line=393'>394</a> ) <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/io/_renderers.py?line=395'>396</a> if not nbformat or LooseVersion(nbformat.__version__) < LooseVersion("4.2.0"): --> <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/io/_renderers.py?line=396'>397</a> raise ValueError( <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/io/_renderers.py?line=397'>398</a> "Mime type rendering requires nbformat>=4.2.0 but it is not installed" <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/io/_renderers.py?line=398'>399</a> ) <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/io/_renderers.py?line=400'>401</a> ipython_display.display(bundle, raw=True) <a href='file:///~/AppData/Local/Programs/Python/Python310/lib/site-packages/plotly/io/_renderers.py?line=402'>403</a> # external renderers ValueError: Mime type rendering requires nbformat>=4.2.0 but it is not installed
fig = px.scatter_geo(covid_df, locations="iso_code", color="continent",
hover_name="location", size="total_cases",
projection="natural earth", animation_frame="date", template="plotly_dark")
fig.show()
import torchvision
import os
import matplotlib.pyplot as plt
from matplotlib import rc
from matplotlib.animation import FuncAnimation
from matplotlib import animation
rc('animation', html='jshtml')
frn = 10 # Number of frames to process in the animation
fps = 0.5 # Frames per second
mywriter = animation.PillowWriter(fps=fps)
mnist_dataset = torchvision.datasets.MNIST(root = "data/mnist", train = True, download = True, transform=torchvision.transforms.ToTensor())
fig, ax = plt.subplots(figsize = (10, 10))
def change_plot(frame_idx):
ax.cla()
image_tensor = mnist_dataset[frame_idx][0]
image_tensor_gray = image_tensor[0]
image_tensor_gray = image_tensor_gray * 255
ax.matshow(image_tensor_gray, cmap = "gray")
for i in range(image_tensor_gray.shape[0]):
for j in range(image_tensor_gray.shape[1]):
ax.text(i, j, str(int(image_tensor_gray[j][i].item())), va = "center", ha = "center", color = "blue", fontsize = "small")
ax.axis("off")
plt.tight_layout()
anim = FuncAnimation(fig, change_plot, frn, interval=1000 / fps)
plt.close()
anim
<Figure size 432x288 with 0 Axes>
mywriter = animation.PillowWriter(fps=fps)
if not os.path.exists("./assets/gif"):
os.makedir("./assets/gif")
anim.save('./assets/gif/mnist.gif',writer=mywriter)
<Figure size 432x288 with 0 Axes>